package com.fhs.trans.extend;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fhs.common.spring.SpringContextUtil;
import com.fhs.core.trans.anno.AutoTrans;
import com.fhs.core.trans.vo.VO;
import com.fhs.trans.service.AutoTransAble;
import com.fhs.trans.service.TransableRegister;
import com.fhs.trans.service.impl.AutoTransService;
import lombok.Data;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 用来注册
 */
@Data
public class JPATransableRegister extends TransableRegister {

    /**
     * service的包路径
     */
    private String packageNames;

    @Autowired
    private AutoTransService autoTransService;


    @Autowired
    private EntityManager em;


    @Override
    protected void regTransable(AutoTransAble autoTransAble, AutoTrans autoTransSett) {
        autoTransService.regTransable(autoTransAble,autoTransSett);
    }

    @Override
    protected void regTransable(Class<? extends VO> entity, AutoTrans autoTransSett) {
        autoTransService.regTransable(new JPATransableAdapter(entity,em),autoTransSett);
    }

    @Override
    protected void apply(List<String> namespaceList) {
        new Thread(() -> {
            Thread.currentThread().setName("refresh auto trans cache");
            for (String namespace : namespaceList) {
                autoTransService.refreshOneNamespace( namespace);
            }
        }).start();
    }

}
